<template>
  <view class="">
    <view v-if="userStore.userInfo.status===1">
      <PatientHome></PatientHome>
    </view>
    <view v-else-if="userStore.userInfo.status===2">
      <PatientHome></PatientHome>
    </view>
    <view v-else-if="userStore.userInfo.status===3">
      <NurseHome></NurseHome>
    </view>
  </view>
</template>

<script setup lang="ts">
import {useUserStore,} from '@/store'

import NurseHome from "@/pages/user/home/components/NurseHome.vue";
import PatientHome from "@/pages/user/home/components/PatientHome.vue";
import {onMounted} from "vue";
import api from "@/api";


const userStore = useUserStore()
const userInfo = userStore.userInfo

const getLocation = () => {
  uni.getLocation({
    type: 'wgs84',
    success: (res) => {
      const latitude = res.latitude
      const longitude = res.longitude;
      api.tencent.location(
          latitude,
          longitude
      ).then(re => {
        if (re.data.status === 0) {
          userInfo.city = re.data.result.address_component.city.replace("市", "")
          userInfo.province = re.data.result.address_component.city.replace("省", "")
          userStore.setUserInfo(userInfo)
        } else {
          console.log("获取信息失败，请重试！")
        }
      })
    },
    fail: (res) => {
      console.log(res)
    },
  })
}
const authorize = () => {
  uni.getSetting({
    success(res) {
      if (res.authSetting['scope.userLocation'] !== undefined && !res.authSetting['scope.userLocation']) {
        uni.showModal({
          title: '请求授权当前位置',
          content: '需要获取您的地理位置，请确认授权',
          success: function (res) {
            if (res.cancel) {
              uni.showToast({
                title: '拒绝授权',
                icon: 'none',
                duration: 1000
              })
            } else if (res.confirm) {
              uni.openSetting({
                success: function (dataAu) {
                  if (dataAu.authSetting["scope.userLocation"]) {
                    //再次授权，调用wx.getLocation的API
                    getLocation()
                  } else {
                    uni.showToast({
                      title: '授权失败',
                      icon: 'none',
                      duration: 1000
                    })
                  }
                }
              })
            }
          }
        })
      } else if (res.authSetting['scope.userLocation'] == undefined) {
        getLocation();
      } else {
        getLocation();
      }
    }
  })
}
onMounted(() => {
  authorize()


})
</script>

<style scoped>

</style>
